
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Javascript,">
  

  
    <meta name="description" content="Javascript数组方法大全">
  
  
  <link rel="icon" type="image/x-icon" href="/logo.png">
  <title>Javascript数组方法大全 [ 复制粘贴工程师 ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="https://wx.qlogo.cn/mmopen/vi_32/AMpRacDgkTPogKk4gQ5IC3QezP55XoPpicQpnD9Vz3eNz6OPFYyFrFzCa4EGQGH65kPia5YJwQBBvvJpVwQ4lfVg/132">
    <span class="title">复制粘贴工程师</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
  </ul>

</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        Javascript数组方法大全
      </h1>
      <span>
        
        <time class="time" datetime="2019-03-14T02:27:35.000Z">
        2019-03-14
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Javascript/">Javascript</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <p>搜罗了目前 <code>Javascript</code> 数组所有的方法，迷失道路的小伙伴可以直接查看页面右边的目录</p>
<h4 id="数组创建"><a href="#数组创建" class="headerlink" title="数组创建"></a>数组创建</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]; <span class="comment">// [1, 2, 3]</span></span><br><span class="line"><span class="keyword">const</span> arr2 = <span class="built_in">Array</span>(); <span class="comment">// []</span></span><br><span class="line"><span class="keyword">const</span> arr3 = <span class="built_in">Array</span>(<span class="number">2</span>); <span class="comment">// [undefined, undefined]</span></span><br><span class="line"><span class="keyword">const</span> arr4 = <span class="built_in">Array</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>);  <span class="comment">// [1, 2, 3]</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h4 id="Array-from-将一个对象转化成数组"><a href="#Array-from-将一个对象转化成数组" class="headerlink" title="Array.from: 将一个对象转化成数组"></a>Array.from: 将一个对象转化成数组</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *  第一个参数：被转化的数组</span></span><br><span class="line"><span class="comment"> *  第二个参数：对每个元素进行处理，将处理后的结果放入返回的数组（类似于map）</span></span><br><span class="line"><span class="comment"> *  第三个参数：用来绑定this</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 当参数为一个Object类型时，需要有length属性，用来指定数组长度，默认为0</span></span><br><span class="line"><span class="built_in">Array</span>.from(&#123; <span class="number">2</span>: <span class="string">'2'</span>, <span class="attr">a</span>: <span class="string">'a'</span>, <span class="number">0</span>: <span class="string">'0'</span>, <span class="attr">length</span>: <span class="number">4</span>&#125;)  <span class="comment">// ['0', undefined, 2: undefined]</span></span><br><span class="line"><span class="built_in">Array</span>.from(&#123; <span class="number">2</span>: <span class="string">'2'</span>, <span class="attr">a</span>: <span class="string">'a'</span>, <span class="number">0</span>: <span class="string">'0'</span>, <span class="attr">length</span>: <span class="number">2</span>&#125;)  <span class="comment">// ['0', undefined]</span></span><br><span class="line"><span class="built_in">Array</span>.from(&#123; <span class="number">2</span>: <span class="string">'2'</span>, <span class="attr">a</span>: <span class="string">'a'</span>, <span class="number">0</span>: <span class="string">'0'</span>, <span class="attr">length</span>: <span class="number">2</span>&#125;, v =&gt; v || <span class="string">''</span>)   <span class="comment">// ['0', '']</span></span><br><span class="line"><span class="built_in">Array</span>.from(<span class="string">'hello'</span>) <span class="comment">// ['h', 'e', 'l', 'l', 'o']</span></span><br></pre></td></tr></table></figure>
<h4 id="Array-isArray-判断传递的值是否是一个-Array-类型"><a href="#Array-isArray-判断传递的值是否是一个-Array-类型" class="headerlink" title="Array.isArray: 判断传递的值是否是一个 Array 类型"></a>Array.isArray: 判断传递的值是否是一个 <code>Array</code> 类型</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.isArray([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])  <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Array</span>.isArray(&#123;<span class="attr">a</span>: <span class="string">'a'</span>&#125;) <span class="comment">// false</span></span><br><span class="line"><span class="built_in">Array</span>.isArray(<span class="string">'hello'</span>)  <span class="comment">// false</span></span><br><span class="line"><span class="built_in">Array</span>.isArray(<span class="built_in">Array</span>(<span class="number">3</span>)) <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Array</span>.isArray(<span class="literal">null</span>)     <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<h4 id="Array-of-把所有的参数放到一个数组中返回"><a href="#Array-of-把所有的参数放到一个数组中返回" class="headerlink" title="Array.of: 把所有的参数放到一个数组中返回"></a>Array.of: 把所有的参数放到一个数组中返回</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = <span class="built_in">Array</span>.of(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>); <span class="comment">// [1, 2, 3]</span></span><br><span class="line"><span class="keyword">const</span> arr1 = <span class="built_in">Array</span>.of();        <span class="comment">// []</span></span><br></pre></td></tr></table></figure>
<h4 id="Array-apply-创建空数组"><a href="#Array-apply-创建空数组" class="headerlink" title="Array.apply: 创建空数组"></a>Array.apply: 创建空数组</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *  第一个参数：用来绑定this</span></span><br><span class="line"><span class="comment"> *  第二个参数：数组参数(参考Array.from的第一个参数)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">Array</span>.apply(<span class="literal">null</span>, &#123;<span class="number">1</span>:<span class="string">'1'</span>, <span class="attr">length</span>: <span class="number">2</span>&#125;) <span class="comment">// [undefined, '1']</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>Array.apply(null, {length: 2}</code> 与 <code>Array(2)</code> 的区别:</p>
<ul>
<li><code>Array(2)</code> 只有 <code>length</code>，没有索引和元素，所以不能用 <code>map</code></li>
</ul>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = <span class="built_in">Array</span>.apply(<span class="literal">null</span>, &#123;<span class="attr">length</span>: <span class="number">2</span>&#125;) <span class="comment">// [undefined, undefined]</span></span><br><span class="line"><span class="keyword">const</span> arr2 = <span class="built_in">Array</span>(<span class="number">2</span>) <span class="comment">// [empty x 2]</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span> <span class="keyword">in</span> arr1 <span class="comment">// true</span></span><br><span class="line"><span class="number">1</span> <span class="keyword">in</span> arr2 <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line">arr1.map(<span class="function"><span class="params">v</span> =&gt;</span> <span class="number">1</span>) <span class="comment">// [1, 1]</span></span><br><span class="line">arr2.map(<span class="function"><span class="params">v</span> =&gt;</span> <span class="number">1</span>) <span class="comment">// [empty x 2]</span></span><br></pre></td></tr></table></figure>
<h4 id="concat-合并多个数组并返回一个新数组-原有数组不变"><a href="#concat-合并多个数组并返回一个新数组-原有数组不变" class="headerlink" title="concat: 合并多个数组并返回一个新数组(原有数组不变)"></a>concat: 合并多个数组并返回一个新数组(原有数组不变)</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"><span class="keyword">const</span> arr2 = [<span class="string">'a'</span>, <span class="string">'b'</span>]</span><br><span class="line"><span class="keyword">const</span> arr3 = [<span class="string">'hello'</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr4 = arr1.concat(arr2, arr3)  <span class="comment">// [1, 2, 'a', 'b', 'hello']</span></span><br></pre></td></tr></table></figure>
<h4 id="copyWithin-从数组的指定位置拷贝元素到另一个指定位置"><a href="#copyWithin-从数组的指定位置拷贝元素到另一个指定位置" class="headerlink" title="copyWithin: 从数组的指定位置拷贝元素到另一个指定位置"></a>copyWithin: 从数组的指定位置拷贝元素到另一个指定位置</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *  第一个参数：复制的目标位置</span></span><br><span class="line"><span class="comment"> *  第二个参数：复制的开始位置(闭区间)</span></span><br><span class="line"><span class="comment"> *  第三个参数：复制的结束位置(开区间)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">arr.copyWithin(<span class="number">4</span>, <span class="number">1</span>, <span class="number">3</span>) <span class="comment">// [1,2,3,4,2,3]</span></span><br></pre></td></tr></table></figure>
<h4 id="entries-返回一个数组的迭代对象-Iterator"><a href="#entries-返回一个数组的迭代对象-Iterator" class="headerlink" title="entries: 返回一个数组的迭代对象(Iterator)"></a>entries: 返回一个数组的迭代对象(Iterator)</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="keyword">const</span> it = arr.entries()</span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: [0,1], done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: [1,2], done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: [2,3], done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: undefined, done: true&#125;</span></span><br></pre></td></tr></table></figure>
<h4 id="every-对数组中所有的元素进行检查，返回布尔值"><a href="#every-对数组中所有的元素进行检查，返回布尔值" class="headerlink" title="every: 对数组中所有的元素进行检查，返回布尔值"></a>every: 对数组中所有的元素进行检查，返回布尔值</h4><h4 id="some-和every类似，every是需要所有元素都满足条件，而some只要有满足的就会返回true"><a href="#some-和every类似，every是需要所有元素都满足条件，而some只要有满足的就会返回true" class="headerlink" title="some: 和every类似，every是需要所有元素都满足条件，而some只要有满足的就会返回true"></a>some: 和every类似，every是需要所有元素都满足条件，而some只要有满足的就会返回true</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr.every(<span class="function"><span class="params">v</span> =&gt;</span> &#123; <span class="built_in">console</span>.log(v); <span class="keyword">return</span> v &lt; <span class="number">10</span>&#125;) <span class="comment">// 1 2 3 true</span></span><br><span class="line">arr.every(<span class="function"><span class="params">v</span> =&gt;</span> &#123; <span class="built_in">console</span>.log(v); <span class="keyword">return</span> v &lt; <span class="number">2</span>&#125;)  <span class="comment">// 1 2 false</span></span><br><span class="line">arr.some(<span class="function"><span class="params">v</span> =&gt;</span> &#123; <span class="built_in">console</span>.log(v); <span class="keyword">return</span> v &lt; <span class="number">2</span>&#125;)  <span class="comment">// 1 true</span></span><br></pre></td></tr></table></figure>
<h4 id="fill-用一个固定值替换数组中的元素"><a href="#fill-用一个固定值替换数组中的元素" class="headerlink" title="fill: 用一个固定值替换数组中的元素"></a>fill: 用一个固定值替换数组中的元素</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *  第一个参数：填充的值</span></span><br><span class="line"><span class="comment"> *  第二个参数：填充的开始位置(闭区间)</span></span><br><span class="line"><span class="comment"> *  第三个参数：填充的结束位置(开区间)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">arr.fill(<span class="number">0</span>, <span class="number">1</span>, <span class="number">3</span>) <span class="comment">// [1,0,0,4,5,6]</span></span><br><span class="line"><span class="built_in">Array</span>(<span class="number">2</span>).fill(<span class="number">0</span>)  <span class="comment">// [0,0]</span></span><br></pre></td></tr></table></figure>
<h4 id="filter-创建一个新数组，返回原数组中满足条件的所有元素"><a href="#filter-创建一个新数组，返回原数组中满足条件的所有元素" class="headerlink" title="filter: 创建一个新数组，返回原数组中满足条件的所有元素"></a>filter: 创建一个新数组，返回原数组中满足条件的所有元素</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">10</span>, <span class="attr">name</span>: <span class="string">'10'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">23</span>, <span class="attr">name</span>: <span class="string">'23'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">16</span>, <span class="attr">name</span>: <span class="string">'16'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">11</span>, <span class="attr">name</span>: <span class="string">'11'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">37</span>, <span class="attr">name</span>: <span class="string">'37'</span>&#125;</span><br><span class="line">]</span><br><span class="line">arr1.filter(<span class="function"><span class="params">v</span> =&gt;</span> v.age &lt; <span class="number">18</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 输出</span></span><br><span class="line"><span class="comment">/* [</span></span><br><span class="line"><span class="comment"> *   &#123; age: 10, name: '10'&#125;,</span></span><br><span class="line"><span class="comment"> *   &#123; age: 16, name: '16'&#125;,</span></span><br><span class="line"><span class="comment"> *   &#123; age: 11, name: '11'&#125;</span></span><br><span class="line"><span class="comment"> * ]</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
<h4 id="find-返回数组中第一个满足条件的元素"><a href="#find-返回数组中第一个满足条件的元素" class="headerlink" title="find: 返回数组中第一个满足条件的元素"></a>find: 返回数组中第一个满足条件的元素</h4><h4 id="findIndex-返回数组中第一个满足条件的元素的索引值"><a href="#findIndex-返回数组中第一个满足条件的元素的索引值" class="headerlink" title="findIndex: 返回数组中第一个满足条件的元素的索引值"></a>findIndex: 返回数组中第一个满足条件的元素的索引值</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">10</span>, <span class="attr">name</span>: <span class="string">'10'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">23</span>, <span class="attr">name</span>: <span class="string">'23'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">16</span>, <span class="attr">name</span>: <span class="string">'16'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">11</span>, <span class="attr">name</span>: <span class="string">'11'</span>&#125;,</span><br><span class="line">  &#123; <span class="attr">age</span>: <span class="number">37</span>, <span class="attr">name</span>: <span class="string">'37'</span>&#125;</span><br><span class="line">]</span><br><span class="line">arr1.find(<span class="function"><span class="params">v</span> =&gt;</span> v.age &gt; <span class="number">18</span>)  <span class="comment">// &#123; age: 23, name: '23'&#125;</span></span><br></pre></td></tr></table></figure>
<h4 id="flat-创建一个新数组，扁平化原嵌套数组，参数为扁平的层数，该方法会移除空项"><a href="#flat-创建一个新数组，扁平化原嵌套数组，参数为扁平的层数，该方法会移除空项" class="headerlink" title="flat: 创建一个新数组，扁平化原嵌套数组，参数为扁平的层数，该方法会移除空项"></a>flat: 创建一个新数组，扁平化原嵌套数组，参数为扁平的层数，该方法会移除空项</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, , [<span class="number">2</span>, [<span class="number">3</span>, , [<span class="number">4</span>]]]]</span><br><span class="line">arr.flat()      <span class="comment">// [1,2,[3, , [4]]]</span></span><br><span class="line">arr.flat(<span class="number">2</span>)     <span class="comment">// [1,2,3,[4]]</span></span><br><span class="line">arr.flat(<span class="number">3</span>)     <span class="comment">// [1,2,3,4]</span></span><br><span class="line">arr.toString()  <span class="comment">// '1,,2,3,,4'</span></span><br></pre></td></tr></table></figure>
<h4 id="flatMap-和-map-相近，但是会将返回结果压平一层"><a href="#flatMap-和-map-相近，但是会将返回结果压平一层" class="headerlink" title="flatMap: 和 map 相近，但是会将返回结果压平一层"></a>flatMap: 和 map 相近，但是会将返回结果压平一层</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr1.map(<span class="function"><span class="params">v</span> =&gt;</span> [v * <span class="number">2</span>])    <span class="comment">// [[2], [4], [6]]</span></span><br><span class="line">arr1.flatMap(<span class="function"><span class="params">v</span> =&gt;</span> [v * <span class="number">2</span>]) <span class="comment">// [2, 4, 6]</span></span><br></pre></td></tr></table></figure>
<h4 id="forEach-遍历数组中的每一个元素"><a href="#forEach-遍历数组中的每一个元素" class="headerlink" title="forEach: 遍历数组中的每一个元素"></a>forEach: 遍历数组中的每一个元素</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr1.forEach(<span class="function"><span class="params">v</span> =&gt;</span> <span class="built_in">console</span>.log(v))  <span class="comment">// 1,2,3</span></span><br></pre></td></tr></table></figure>
<h4 id="includes-判断一个数组中是否包含一个元素"><a href="#includes-判断一个数组中是否包含一个元素" class="headerlink" title="includes: 判断一个数组中是否包含一个元素"></a>includes: 判断一个数组中是否包含一个元素</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;<span class="attr">id</span>: <span class="number">2</span>, <span class="attr">value</span>: <span class="string">'2'</span>&#125;</span><br><span class="line"><span class="keyword">const</span> arr = [</span><br><span class="line">  obj,</span><br><span class="line">  <span class="string">'hello'</span>,</span><br><span class="line">  &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">value</span>: <span class="string">'1'</span>&#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">arr.includes(&#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">value</span>: <span class="string">'1'</span>&#125;) <span class="comment">// false</span></span><br><span class="line">arr.includes(obj) <span class="comment">// true</span></span><br><span class="line">arr.includes(<span class="string">'hello'</span>) <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<h4 id="indexOf-找到满足条件的元素的索引值，第一个参数是开始寻找的位置"><a href="#indexOf-找到满足条件的元素的索引值，第一个参数是开始寻找的位置" class="headerlink" title="indexOf: 找到满足条件的元素的索引值，第一个参数是开始寻找的位置"></a>indexOf: 找到满足条件的元素的索引值，第一个参数是开始寻找的位置</h4><h4 id="lastIndexOf-寻找顺序与indexOf相反"><a href="#lastIndexOf-寻找顺序与indexOf相反" class="headerlink" title="lastIndexOf: 寻找顺序与indexOf相反"></a>lastIndexOf: 寻找顺序与indexOf相反</h4><blockquote>
<p><code>findIndex</code> 的能力比 <code>indexOf</code> 更强，<code>findIndex</code> 相当于一个 <code>for</code> 循环，可以进行逻辑处理和判断，而 <code>indexOf</code> 能力较弱</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>,<span class="string">'b'</span>]</span><br><span class="line">arr1.indexOf(<span class="string">'b'</span>) <span class="comment">// 1</span></span><br><span class="line">arr1.lastIndexOf(<span class="string">'b'</span>) <span class="comment">// 3</span></span><br></pre></td></tr></table></figure>
<h4 id="join-将元素连接成一个字符串，并用分隔符分割（第一个参数），元素会执行-toString"><a href="#join-将元素连接成一个字符串，并用分隔符分割（第一个参数），元素会执行-toString" class="headerlink" title="join: 将元素连接成一个字符串，并用分隔符分割（第一个参数），元素会执行 toString"></a>join: 将元素连接成一个字符串，并用分隔符分割（第一个参数），元素会执行 toString</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="string">'b'</span>, &#123;<span class="attr">c</span>: <span class="number">2</span>&#125;, [<span class="number">3</span>,<span class="number">4</span>]]</span><br><span class="line">arr.join(<span class="string">' &amp; '</span>) <span class="comment">// "1 &amp; b &amp; [object Object] &amp; 3,4"</span></span><br></pre></td></tr></table></figure>
<h4 id="keys-和-entries-类似，但是返回的为-索引，而不再是-key-value"><a href="#keys-和-entries-类似，但是返回的为-索引，而不再是-key-value" class="headerlink" title="keys: 和 entries 类似，但是返回的为 索引，而不再是 key/value"></a>keys: 和 entries 类似，但是返回的为 索引，而不再是 key/value</h4><h4 id="values-和-entries-类似，但是返回的为-值，而不再是-key-value"><a href="#values-和-entries-类似，但是返回的为-值，而不再是-key-value" class="headerlink" title="values: 和 entries 类似，但是返回的为 值，而不再是 key/value"></a>values: 和 entries 类似，但是返回的为 值，而不再是 key/value</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="keyword">const</span> it = arr.keys()</span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: 0, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: 1, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: 2, done: false&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(it.next())  <span class="comment">// &#123;value: undefined, done: true&#125;</span></span><br></pre></td></tr></table></figure>
<h4 id="map-返回一个新数组，数组中的元素为原数组经过处理后的值"><a href="#map-返回一个新数组，数组中的元素为原数组经过处理后的值" class="headerlink" title="map: 返回一个新数组，数组中的元素为原数组经过处理后的值"></a>map: 返回一个新数组，数组中的元素为原数组经过处理后的值</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [&#123;<span class="attr">id</span>:<span class="number">1</span>&#125;, &#123;<span class="attr">id</span>:<span class="number">2</span>&#125;, &#123;<span class="attr">id</span>: <span class="number">3</span>&#125;]</span><br><span class="line">arr1.map(<span class="function"><span class="params">v</span> =&gt;</span> v.id) <span class="comment">// [1, 2, 3]</span></span><br></pre></td></tr></table></figure>
<h4 id="pop-删除原数组的最后一个元素"><a href="#pop-删除原数组的最后一个元素" class="headerlink" title="pop: 删除原数组的最后一个元素"></a>pop: 删除原数组的最后一个元素</h4><h4 id="push-在原数组的末尾添加一个元素"><a href="#push-在原数组的末尾添加一个元素" class="headerlink" title="push: 在原数组的末尾添加一个元素"></a>push: 在原数组的末尾添加一个元素</h4><h4 id="shift-删除原数组的第一个元素"><a href="#shift-删除原数组的第一个元素" class="headerlink" title="shift: 删除原数组的第一个元素"></a>shift: 删除原数组的第一个元素</h4><h4 id="unshift-在原数组开头添加一个元素"><a href="#unshift-在原数组开头添加一个元素" class="headerlink" title="unshift: 在原数组开头添加一个元素"></a>unshift: 在原数组开头添加一个元素</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr.pop()       <span class="comment">// 3; arr: [1,2]</span></span><br><span class="line">arr.shift()     <span class="comment">// 1; arr: [2]</span></span><br><span class="line">arr.push(<span class="number">3</span>)     <span class="comment">// 2; arr: [2,3]</span></span><br><span class="line">arr.unshift(<span class="number">4</span>)  <span class="comment">// 2; arr: [4,2,3]</span></span><br></pre></td></tr></table></figure>
<h4 id="reduce-接受一个累加器，数组中的每个值开始缩减，最终计算为一个值"><a href="#reduce-接受一个累加器，数组中的每个值开始缩减，最终计算为一个值" class="headerlink" title="reduce: 接受一个累加器，数组中的每个值开始缩减，最终计算为一个值"></a>reduce: 接受一个累加器，数组中的每个值开始缩减，最终计算为一个值</h4><h4 id="reduceRight-功能和reduce一样，只是是从末尾向前做累加"><a href="#reduceRight-功能和reduce一样，只是是从末尾向前做累加" class="headerlink" title="reduceRight: 功能和reduce一样，只是是从末尾向前做累加"></a>reduceRight: 功能和reduce一样，只是是从末尾向前做累加</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSum</span>(<span class="params">total, currentValue, currentIndex, arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> total + currentValue;</span><br><span class="line">&#125;</span><br><span class="line">arr.reduce(getSum, <span class="number">10</span>)  <span class="comment">// 16; arr: [1,2,3]</span></span><br></pre></td></tr></table></figure>
<h4 id="reverse-颠倒原数组中元素的顺序"><a href="#reverse-颠倒原数组中元素的顺序" class="headerlink" title="reverse: 颠倒原数组中元素的顺序"></a>reverse: 颠倒原数组中元素的顺序</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr.reverse() <span class="comment">// [3,2,1]</span></span><br></pre></td></tr></table></figure>
<h4 id="slice-提取数组中的子串到新数组"><a href="#slice-提取数组中的子串到新数组" class="headerlink" title="slice: 提取数组中的子串到新数组"></a>slice: 提取数组中的子串到新数组</h4><h4 id="splice-删除原数组中的子串"><a href="#splice-删除原数组中的子串" class="headerlink" title="splice: 删除原数组中的子串"></a>splice: 删除原数组中的子串</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">arr.slice(<span class="number">2</span>,<span class="number">4</span>)  <span class="comment">// [3,4]; arr: [1,2,3,4,5]</span></span><br><span class="line">arr.splice(<span class="number">1</span>,<span class="number">2</span>) <span class="comment">// [2,3]; arr: [1,4,5]</span></span><br></pre></td></tr></table></figure>
<h4 id="sort"><a href="#sort" class="headerlink" title="sort"></a>sort</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> asc = <span class="function">(<span class="params">a,b</span>) =&gt;</span> a - b</span><br><span class="line"><span class="keyword">const</span> des = <span class="function">(<span class="params">a,b</span>) =&gt;</span> b - a</span><br><span class="line"><span class="keyword">const</span> arr = [<span class="number">3</span>,<span class="number">1</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">7</span>]</span><br><span class="line">arr.sort(asc) <span class="comment">// [1, 2, 3, 4, 7]</span></span><br><span class="line">arr.sort(des) <span class="comment">// [7, 4, 3, 2, 1]</span></span><br></pre></td></tr></table></figure>
    </div>

    <div>全文完。</div>
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#数组创建"><span class="toc-text">数组创建</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Array-from-将一个对象转化成数组"><span class="toc-text">Array.from: 将一个对象转化成数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Array-isArray-判断传递的值是否是一个-Array-类型"><span class="toc-text">Array.isArray: 判断传递的值是否是一个 Array 类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Array-of-把所有的参数放到一个数组中返回"><span class="toc-text">Array.of: 把所有的参数放到一个数组中返回</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Array-apply-创建空数组"><span class="toc-text">Array.apply: 创建空数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#concat-合并多个数组并返回一个新数组-原有数组不变"><span class="toc-text">concat: 合并多个数组并返回一个新数组(原有数组不变)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#copyWithin-从数组的指定位置拷贝元素到另一个指定位置"><span class="toc-text">copyWithin: 从数组的指定位置拷贝元素到另一个指定位置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#entries-返回一个数组的迭代对象-Iterator"><span class="toc-text">entries: 返回一个数组的迭代对象(Iterator)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#every-对数组中所有的元素进行检查，返回布尔值"><span class="toc-text">every: 对数组中所有的元素进行检查，返回布尔值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#some-和every类似，every是需要所有元素都满足条件，而some只要有满足的就会返回true"><span class="toc-text">some: 和every类似，every是需要所有元素都满足条件，而some只要有满足的就会返回true</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#fill-用一个固定值替换数组中的元素"><span class="toc-text">fill: 用一个固定值替换数组中的元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#filter-创建一个新数组，返回原数组中满足条件的所有元素"><span class="toc-text">filter: 创建一个新数组，返回原数组中满足条件的所有元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#find-返回数组中第一个满足条件的元素"><span class="toc-text">find: 返回数组中第一个满足条件的元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#findIndex-返回数组中第一个满足条件的元素的索引值"><span class="toc-text">findIndex: 返回数组中第一个满足条件的元素的索引值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#flat-创建一个新数组，扁平化原嵌套数组，参数为扁平的层数，该方法会移除空项"><span class="toc-text">flat: 创建一个新数组，扁平化原嵌套数组，参数为扁平的层数，该方法会移除空项</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#flatMap-和-map-相近，但是会将返回结果压平一层"><span class="toc-text">flatMap: 和 map 相近，但是会将返回结果压平一层</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#forEach-遍历数组中的每一个元素"><span class="toc-text">forEach: 遍历数组中的每一个元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#includes-判断一个数组中是否包含一个元素"><span class="toc-text">includes: 判断一个数组中是否包含一个元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#indexOf-找到满足条件的元素的索引值，第一个参数是开始寻找的位置"><span class="toc-text">indexOf: 找到满足条件的元素的索引值，第一个参数是开始寻找的位置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#lastIndexOf-寻找顺序与indexOf相反"><span class="toc-text">lastIndexOf: 寻找顺序与indexOf相反</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#join-将元素连接成一个字符串，并用分隔符分割（第一个参数），元素会执行-toString"><span class="toc-text">join: 将元素连接成一个字符串，并用分隔符分割（第一个参数），元素会执行 toString</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#keys-和-entries-类似，但是返回的为-索引，而不再是-key-value"><span class="toc-text">keys: 和 entries 类似，但是返回的为 索引，而不再是 key/value</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#values-和-entries-类似，但是返回的为-值，而不再是-key-value"><span class="toc-text">values: 和 entries 类似，但是返回的为 值，而不再是 key/value</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#map-返回一个新数组，数组中的元素为原数组经过处理后的值"><span class="toc-text">map: 返回一个新数组，数组中的元素为原数组经过处理后的值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#pop-删除原数组的最后一个元素"><span class="toc-text">pop: 删除原数组的最后一个元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#push-在原数组的末尾添加一个元素"><span class="toc-text">push: 在原数组的末尾添加一个元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#shift-删除原数组的第一个元素"><span class="toc-text">shift: 删除原数组的第一个元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#unshift-在原数组开头添加一个元素"><span class="toc-text">unshift: 在原数组开头添加一个元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#reduce-接受一个累加器，数组中的每个值开始缩减，最终计算为一个值"><span class="toc-text">reduce: 接受一个累加器，数组中的每个值开始缩减，最终计算为一个值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#reduceRight-功能和reduce一样，只是是从末尾向前做累加"><span class="toc-text">reduceRight: 功能和reduce一样，只是是从末尾向前做累加</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#reverse-颠倒原数组中元素的顺序"><span class="toc-text">reverse: 颠倒原数组中元素的顺序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#slice-提取数组中的子串到新数组"><span class="toc-text">slice: 提取数组中的子串到新数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#splice-删除原数组中的子串"><span class="toc-text">splice: 删除原数组中的子串</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#sort"><span class="toc-text">sort</span></a></li></ol>
  </div>


  </div>
</div>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share" style="width: 100%;">
  <!-- <img src="https://kevinofneu-blog-static.oss-cn-beijing.aliyuncs.com/static/2018-12-10-qrcode_for_gh_ffacf5722095_258.jpg" alt="Running Geek" style="margin: auto; display: block;"/>

  <div style="margin: auto; text-align: center; font-size: 0.8em; color: grey;">老铁们关注走一走，不迷路</div> -->

</div>

  
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2019/03/04/微信小程序中实现状态管理/" rel="next" title="微信小程序中实现状态管理">
          微信小程序中实现状态管理
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2019/03/15/Javascript Object方法大全/" rel="prev" title="Javascript Object 常用方法大全">
            Javascript Object 常用方法大全
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


    </div>

    
    <div id="vcomments" style="max-width: 800px; margin: 16px auto;"></div>
  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="/">首页</a> |
        <a class="bottom-item" href="https://github.com/TJ-XiaJiaHao" target="_blank">GitHub</a> |
        <a class="bottom-item">联系邮箱：1452806@tongji.edu.cn</a>
    </div>
</footer>
  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script type="text/javascript">
  new Valine({
    el: '#vcomments',
    appId: '8CwYqXS8IoFbvaA9PmgqGjTf-gzGzoHsz',
    appKey: 'z5bVoSPDPhd8hzlXvDey5Hpz',
    verify:false,
    placeholder: '评论留言'
  })
</script>
</html>
